package com.it.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component("myAspect")
@Aspect
public class MyAspect {

    @Pointcut("execution(public * com.it.service.impl.*.*(..))")
    public void pointcut(){};

    @Before("pointcut()")
    public void beforeMethod() {
        System.out.println("前置通知。。。");
    }

    @AfterReturning("pointcut()")
    public void afterReturing() {
        System.out.println("后置通知。。。");
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing() {
        System.out.println("异常通知。。。");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("最终通知。。。");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {
        System.out.println("前置。。。。");

        //获取参数
        Object[] args = pjp.getArgs();
        System.out.println(Arrays.toString(args));

        Object proceed=null;
        try {
            //调用执行目标对象方法
            proceed = pjp.proceed(args);
            System.out.println("后置。。。");
        } catch (Throwable throwable) {
            System.out.println("异常。。。。");
        }finally {
            System.out.println("最终...");
        }
        return proceed;
    }
}
